package com.ffzx.commerce.framework.exception;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 *************************************************************** 
 * <p>
 * 
 * @DESCRIPTION : 基本运行时异常
 * @AUTHOR : zhu.gj
 * @DATE :2016-1-26
 *       </p>
 **************************************************************** 
 */
public class FFZXBaseException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1325426932819373813L;

	/**
	 * 错误代码
	 */
	private String errCode;

	public String getErrCode() {
		return errCode;
	}

	public FFZXBaseException(String message) {
		super(message);
	}

	public FFZXBaseException(String code, String message) {
		super(message);
		errCode = code;
	}

	public FFZXBaseException(String code, String message, Throwable cause) {
		super(message, cause);
		errCode = code;
	}

	public String getReadableMessage() {
		return getMessage();
	}

	public String getMessageWithSupportCode() {
		if (StringUtils.isNotEmpty(getErrCode())) {
			StringBuilder builder = new StringBuilder();
			builder.append(getReadableMessage()).append("[").append(getErrCode()).append("]");
			return builder.toString();
		}
		return getReadableMessage();
	}

	public String getMessageWithSupportCode(Exception ex) {
		if (FFZXBaseException.class.isAssignableFrom(ex.getClass()) && StringUtils.isNotEmpty(getErrCode())) {
			StringBuilder builder = new StringBuilder();
			builder.append(getReadableMessage()).append("[").append(getErrCode()).append("]");
			return builder.toString();
		}
		return getReadableMessage();
	}

	public static String genMsg(Exception ex) {

		if (FFZXBaseException.class.isAssignableFrom(ex.getClass())) {

			FFZXBaseException fssBaseEx = (FFZXBaseException) ex;

			if (StringUtils.isNotEmpty(fssBaseEx.getErrCode())) {
				StringBuilder builder = new StringBuilder();
				builder.append(fssBaseEx.getReadableMessage()).append("[").append(fssBaseEx.getErrCode()).append("]");
				return builder.toString();
			} else {
				return ex.getMessage();
			}

		}
		return ex.getMessage();
	}
}
